package cn.tedu.bece.admin.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.tedu.bece.admin.common.exception.ServiceException;
import cn.tedu.bece.admin.system.mapper.SysMenuMapper;
import cn.tedu.bece.admin.system.mapper.SysRoleMenuMapper;
import cn.tedu.bece.admin.system.pojo.dto.SysMenuAddParam;
import cn.tedu.bece.admin.system.pojo.dto.SysMenuUpdateParam;
import cn.tedu.bece.admin.system.pojo.entity.SysMenu;
import cn.tedu.bece.admin.system.pojo.vo.SysMenuTreeVO;
import cn.tedu.bece.admin.system.service.ISysMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class SysMenuServiceImpl implements ISysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public void insert(SysMenuAddParam sysMenuAddParam) {
        SysMenu entity = new SysMenu();
        BeanUtils.copyProperties(sysMenuAddParam, entity);
        sysMenuMapper.insert(entity);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ServiceException("参数不能为空");
        }
        ids.forEach(id -> {
            Long count = sysMenuMapper.selectCount(Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getParentId, id));
            if (count > 0) {
                throw new ServiceException("(" + id + ")菜单下有子菜单，无法删除");
            }
        });
        // 删除角色和菜单关联
        sysRoleMenuMapper.deleteRoleMenuByMenuIds(ids);
        // 删除菜单信息
        sysMenuMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(SysMenuUpdateParam sysMenuUpdateParam) {
        SysMenu entity = new SysMenu();
        BeanUtils.copyProperties(sysMenuUpdateParam, entity);
        sysMenuMapper.updateById(entity);
    }

    @Override
    public SysMenu selectById(Long id) {
        return sysMenuMapper.selectById(id);
    }

    @Override
    public List<SysMenuTreeVO> selectSysMenuTreeVOList() {
        List<SysMenu> sysMenuList = sysMenuMapper.selectList(null);
        return menuList2MenuTreeVOList(sysMenuList);
    }

    private List<SysMenuTreeVO> menuList2MenuTreeVOList(List<SysMenu> sysMenuList) {
        List<SysMenuTreeVO> sysMenuTreeVOList = new ArrayList<>();
        sysMenuList.forEach(sysMenu -> {
            SysMenuTreeVO sysMenuTreeVO = new SysMenuTreeVO();
            BeanUtils.copyProperties(sysMenu, sysMenuTreeVO);
            sysMenuTreeVOList.add(sysMenuTreeVO);
        });
        return buildTree(sysMenuTreeVOList, 0L);
    }

    private List<SysMenuTreeVO> buildTree(List<SysMenuTreeVO> list, Long parentId) {
        List<SysMenuTreeVO> sysMenuTreeVOList = null;
        for (SysMenuTreeVO item : list) {
            if (parentId.equals(item.getParentId())) {
                if (sysMenuTreeVOList == null) {
                    sysMenuTreeVOList = new ArrayList<>();
                }
                sysMenuTreeVOList.add(item);
                item.setChildren(buildTree(list, item.getId()));
            }
        }
        return sysMenuTreeVOList;
    }

    @Override
    public List<SysMenuTreeVO> selectSysMenuTreeVOListByUserId(Long userId) {
        List<SysMenu> sysMenuList = sysMenuMapper.selectListByUserId(userId);
        return menuList2MenuTreeVOList(sysMenuList);
    }

}
